home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / DN114_3D.ZIP / WC_EX1.C < prev   
Encoding:
C/C++ Source or Header  |  1996-02-03  |  3.6 KB  |  151 lines

  1. /*
  2.  
  3.   "Introduction to 3D Programming" Tutorial series
  4.   Article #1 - For DemoNews 114
  5.  
  6.   Sample code for Watcom C 10.0 or above, for 32-bit DOS.
  7.  
  8.   Compile with: WCC386 -bt=dos WC_EX1
  9.         (other command line switches can be added if you wish)
  10.  
  11.   Link with: WLINK system dos4g file WC_EX1
  12.          (or use pmodew if you have it and wish to use it)
  13.  
  14. */
  15.  
  16. #include <stdlib.h>
  17. #include <conio.h>
  18.  
  19.  
  20. /* "vector" structure - holds a 3D cartesian point, and its projection. */
  21. typedef struct vector
  22. {
  23. int x;
  24. int y;
  25. int z;
  26. int scrx;
  27. int scry;
  28. } vector;
  29.  
  30. /* Box object, used for the example. */
  31.  
  32. vector Box[8] =  {
  33.          { 20, 40, 30, 0,0},
  34.          { 20, 40,-30, 0,0},
  35.          { 20,-40, 30, 0,0},
  36.          { 20,-40,-30, 0,0},
  37.          {-20, 40, 30, 0,0},
  38.          {-20, 40,-30, 0,0},
  39.          {-20,-40, 30, 0,0},
  40.          {-20,-40,-30, 0,0}
  41.          };
  42.  
  43. /*
  44.  
  45. Videomode and Putpixel functions for graphics display
  46.  
  47. (Yes, the putpixel is not optimized, but that's not the point of
  48.  this example program :-)
  49.  
  50. */
  51.  
  52. void videomode(unsigned short int mode);
  53. #pragma aux videomode = \
  54.     "int 10h"    \
  55.     parm [ax]    \
  56.     modify [ax];
  57.  
  58. void putpixel(unsigned int x, unsigned int y, char color);
  59. #pragma aux putpixel =      \
  60.     "shl eax, 6"        \
  61.     "mov ebx, eax"        \
  62.     "shl eax, 2"        \
  63.     "add eax, ebx"        \
  64.     "add eax, ecx"        \
  65.     "add eax, 0a0000h"    \
  66.     "mov edi, eax"        \
  67.     "mov [edi], dl"        \
  68.     parm [ecx] [eax] [dl]    \
  69.     modify [eax ebx edi];
  70.  
  71.  
  72.  
  73. /* Function Declarations for PointProject and DrawBox */
  74.  
  75. void PointProject(int distance, vector point, vector *dest, vector center);
  76. void DrawBox(int cenx, int ceny, int cenz, char color);
  77.  
  78.  
  79.  
  80. /*
  81. Main program - Draws three boxes at different 3D centers, showing how
  82.            the depth perception works.
  83. */
  84.  
  85. void main(void)
  86. {
  87.   videomode(0x13);
  88.  
  89.   DrawBox(-60, 0, -40, 13);  /* Purple box, Z=-40 so it's further away */
  90.   DrawBox(  0, 0,   0, 14);  /* Yellow box, Z=0 so it's at the image plane */
  91.   DrawBox( 60, 0,  40, 15);  /* White box,  Z=40 so it's closer to you */
  92.  
  93.   while (!kbhit());
  94.   videomode(0x03);
  95. }
  96.  
  97.  
  98. /*
  99.  
  100. void PointProject(int distance, vector point, vector *dest, vector center)
  101.  
  102. Takes a viewing distance from the origin along the Z axis (in our examples
  103. we've been using 256, so that's what I used in the main program), and the
  104. 3D point you want to project.  Fills in the 2D scrx and scry values of the
  105. given vector, or a separate "after projection" vector, depending on what
  106. you pass in *dest (I used the same vector for this example).
  107.  
  108. The center vector parameter is just added to the point you give.  Stupid
  109. by itself, yes... but if you use the same center for the set of points in
  110. an object (I used a box in this program), the whole object is moved (called
  111. "translation") by the center.  Just run the example, and you'll see what I
  112. mean. :-)
  113.  
  114. */
  115.  
  116.  
  117. void PointProject(int distance, vector point, vector *dest, vector center)
  118. {
  119.   dest->scrx = (256*(point.x+center.x) / (distance-(point.z+center.z))) + 160;
  120.   dest->scry = 100 - (256*(point.y+center.y) / (distance-(point.z+center.z)));
  121. }
  122.  
  123. /*
  124.  
  125. void DrawBox(int cenx, int ceny, int cenz, char color)
  126.  
  127. Draws the Box object with the 3D center you give.  The center points are
  128. merged into the one "center" vector that PointProject wants.  All the points
  129. use the same center, which "moves" the object.
  130.  
  131. */
  132.  
  133. void DrawBox(int cenx, int ceny, int cenz, char color)
  134. {
  135.   int count;
  136.   vector BoxCenter;
  137.  
  138.   BoxCenter.x = cenx;
  139.   BoxCenter.y = ceny;
  140.   BoxCenter.z = cenz;
  141.  
  142.   for (count=0; count<8; count++)
  143.   {
  144.     PointProject(256, Box[count], &Box[count], BoxCenter);
  145.     putpixel(Box[count].scrx, Box[count].scry, color);
  146.   }
  147. }
  148.  
  149.  
  150.  
  151.